package com.example.campus.search.controller;

import cn.hutool.json.JSONUtil;
import com.copyctrl.campus.common.domain.PassageDTO;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @author:JH
 * @version:1.0
 */
@RestController
@RequestMapping("/delete")
public class DeleteController {
    @DeleteMapping ("/{passage}")
    public void delete(@PathVariable String passage){
        PassageDTO passageDTO = JSONUtil.toBean(passage, PassageDTO.class);
        // 1.准备Request      // DELETE /hotel/_doc/{id}
        DeleteRequest request = new DeleteRequest("passages", passageDTO.getId().toString());
        try(RestHighLevelClient client= new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://1.14.107.219:9200")
        ));) {
            // 3.发送请求
            client.delete(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
